package com.itheima.web;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SmsUtil;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @author: libin
 * @date: 2019/11/28 19:57
 * @version: 1.0
 * @describe: 提供方法发送短信验证码，并将验证码保存到redis
 */
@RequestMapping("/validateCode")
@RestController
public class ValidateCodeController {
    @Autowired
    private JedisPool jedisPool;

    /**
     * 提供方法发送短信验证码，并将验证码保存到redis
     *
     * @param telephone
     * @return
     */
    @RequestMapping("/send4Order")
    public Result send4Order(String telephone) {
        //1.调用工具类，随机生成验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        //2.调用工具类发短信
        //SmsUtil.sendSmsCode(telephone, code.toString());
        //3.把验证码存入redis
        Jedis resource = jedisPool.getResource();
        /*3.1存入redis,key值如果为电话号码，如果其他业务也用到电话号码当key存入redis中，
        会覆盖，在电话号码后加业务码在存储*/
        resource.setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,60*60*24,code.toString());
        resource.close();
        return new  Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);

    }

    /**
     * 用户登陆
     * @param telephone
     * @return
     */
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone) {
        //1.调用工具类，随机生成验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        //2.调用工具类发短信
        //SmsUtil.sendSmsCode(telephone, code.toString());
        //3.把验证码存入redis
        Jedis resource = jedisPool.getResource();
        /*3.1存入redis,key值如果为电话号码，如果其他业务也用到电话号码当key存入redis中，
        会覆盖，在电话号码后加业务码在存储*/
        resource.setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,60*60*24,code.toString());
        resource.close();
        return new  Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);

    }

}
